package com.cloud.bc.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.cloud.bc.model.domain.SugerResp;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcFinances;
import com.cloud.bc.model.entity.BcPaymenorders;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.entity.BcUsers;
import com.cloud.bc.model.enums.ConfigEnum;
import com.cloud.bc.model.request.RechargeRequest;
import com.cloud.bc.service.BcConfigsService;
import com.cloud.bc.service.BcFinancesService;
import com.cloud.bc.service.BcPaymenordersService;
import com.cloud.bc.service.BcUsersService;
import com.fasterxml.jackson.databind.ser.Serializers;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

@AllArgsConstructor
@Controller
@RequestMapping("m/center/account")
public class MCenterAccountController extends BaseController {

    private final BcConfigsService bcConfigsService;
    private final BcPaymenordersService bcPaymenordersService;
    private final BcUsersService bcUsersService;
    private final BcFinancesService bcFinancesService;

    /**
     * 钱包
     */
    @GetMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "钱包", "m/center/account/index");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcFinances bcFinances = bcFinancesService.selectOne(new BcFinances().setUserId(userInfo.getId()));
        if (null == bcFinances) {
            bcFinances = new BcFinances();
            bcFinances.setMoney(BigDecimal.ZERO);
            bcFinances.setCandyPrice(BigDecimal.ZERO);
        }
        view.addObject("bcFinances", bcFinances);
        return view;
    }

    /**
     * 充值
     */
    @GetMapping("deposit/candy")
    public ModelAndView depositCandy(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "充值", "m/center/account/deposit/candy");
        String chargeData = bcConfigsService.getValue(ConfigEnum.DEPOSITCANDYAMOUNTS.getTitle());
        ArrayList<SugerResp> sugerList = new ArrayList();
        for (String item : chargeData.split(",")) {
            Integer money = Convert.toInt(item);
            SugerResp sugerResp = new SugerResp();
            sugerResp.setKey(money);
            sugerResp.setValue(NumberUtil.round(NumberUtil.div(Convert.toBigDecimal(money), 100), 2));
            sugerList.add(sugerResp);
        }
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcFinances bcFinances = bcFinancesService.selectOne(new BcFinances().setUserId(userInfo.getId()));
        if (null == bcFinances) {
            bcFinances = new BcFinances();
            bcFinances.setMoney(BigDecimal.ZERO);
            bcFinances.setCandyPrice(BigDecimal.ZERO);
        }
        view.addObject("bcFinances", bcFinances);
        view.addObject("sugerList", sugerList);
        return view;
    }

    /**
     * 交易记录
     */
    @GetMapping("details")
    public ModelAndView details(HttpServletRequest request, HttpServletResponse response) {
        String currency = request.getParameter("currency");
        ModelAndView view = baseView(request, "交易记录", "m/center/account/details");
        view.addObject("requestUrl", StrUtil.isEmpty(currency) ? "/m/ajax/financerecords/list" : "/m/ajax/financerecords/list?type=" + currency);
        return view;
    }

    /**
     * 喜糖兑换
     */
    @GetMapping("exchange")
    public ModelAndView exchange(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "喜糖兑换", "m/center/account/exchange");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcFinances bcFinances = bcFinancesService.selectOne(new BcFinances().setUserId(userInfo.getId()));
        if (null == bcFinances) {
            bcFinances.setCandyPrice(BigDecimal.ZERO);
            bcFinances.setMoney(BigDecimal.ZERO);
        }
        view.addObject("bcFinances", bcFinances);
        return view;
    }

}
